// JavaScript Document
$(function () {
	//保存定时器ID
	var tid=null;

//延迟执行函数
	function debounce(fn,wait){
		//设定默认的延迟时间
		wait=wait||500;
		//清除定时器
		tid && clearTimeout(tid);
		//定时器执行
		tid=setTimeout(fn,wait);
	}

	var seat=null
	var priceSum=0
	getSeatInfo();
	function getSeatInfo() {

		var time_id=$("#timeId").val()
		//alert(time_id)
		$.ajax({
			type:"post",
			data:{time_id:time_id},
			url:"http://127.0.0.1:8080/findSeatInfoByTimesId",
			success:function(data){
			//alert(data.data)
				var count=0;
				seat=data.data
				var seatArr=data.data.split(",")
				for (var i=0;i<seatArr.length;i++){
					var rowArr=seatArr[i].split("")
					for (var j=0;j<rowArr.length;j++){
						if(rowArr[j]==1){
							//alert(i+":"+j)
							$("#seatList li:eq("+i+") img:eq("+j+") ").attr("src","../img/seat_ordered.png")
							count++;
						}
					}
				}
				$("#seatCount").text("本影厅共有座位96个，当前已售"+count+"个")
				/*$($($("#left1").children("ol").children("li").get(0)).children("img").get(0)).attr("src","../img/seat_selected.png")*/

			},

			error:function () {
				alert("错误");
			}
		});
	}
	$("#aCinema").addClass("clickOn")
	$("#menu a").on("click",function(){
		window.location.replace("show_main")
	})
	$("#cinemaMenuSelect a").on("click",function(){
		$(this).siblings("a").removeClass("cinemaSelected")
		$(this).addClass("cinemaSelected")
	})	
	$("#timeSelect a").on("click",function(){
		$(this).siblings("a").removeClass("cinemaSelected")
		$(this).addClass("cinemaSelected")
	})		
	$("#left1 ol li img").click(function () {

	var hang = $(this).parent("li").index()+1;	
    var lie = $(this).index()+1;
    //alert("行："+hang+"列："+lie);
    if($(this).attr("src")=="../img/seat_ordered.png"){
		alert("此座位已被他人锁定")
	}else if($(this).attr("src")=="../img/seat_selected.png"){
		alert("此座位已选择")
	}
    else if($(".seatItem").length<3){
		$(this).attr("src","../img/seat_selected.png")
		$("#seatPos").append("<div class=\"seatItem\" >"+hang+"排"+lie+"座</div>")
		$("#numOrdered h4").text(parseInt($("#numOrdered h4").text())+1)
		var priceCount=$("#priceCount").text()
		priceCount=priceCount.substring(0,priceCount.length-1)
		$("#priceCount").text(priceCount+(parseInt($("#numOrdered h4").text())))
		var counter=$("#counter").text()
		counter=counter.substring(1,counter.length)
		var price=$("#priceCount").text()
		price=price.substring(3,price.length-2)
		priceSum=parseFloat(price)+parseFloat(counter)
		$("#counter").text("￥"+priceSum)
	}else {
    	alert("一个用户最多只能购买三张票哦")
	}

 	});
	$(document).on("click",".seatItem",function () {
		var hang = $(this).text().substring(0,$(this).text().indexOf("排"));
		var lie = $(this).text().substring(2,$(this).text().indexOf("座"));
		//alert("行："+hang+"列："+lie);
		$("#numOrdered h4").text(parseInt($("#numOrdered h4").text())-1)
		var priceCount=$("#priceCount").text()
		priceCount=priceCount.substring(0,priceCount.length-1)
		$("#priceCount").text(priceCount+(parseInt($("#numOrdered h4").text())))
		var counter=$("#counter").text()
		counter=counter.substring(1,counter.length)
		var price=$("#priceCount").text()
		price=price.substring(3,price.length-2)
		priceSum=parseFloat(price)-parseFloat(counter)
		$("#counter").text("￥"+priceSum)
		$("#seatList li:eq("+(hang-1)+") img:eq("+(lie-1)+") ").attr("src","../img/seat.png")
		$(this).remove()
	})
/*	$("#numOrdered").hover(function(){
		$(".seatItem").show()
	})*/
	$("#btnConfirm").on("click",function(){
		if($(".seatItem").length==0){
			alert("你还没有选择任何座位")
		}else {
			$("#one").hide();
			$("#two").show();
			$("#xiaoJi").text("￥"+priceSum)
			$("#Sum").text("￥"+priceSum)
			$("#pro2").addClass("over")
			$("#SeatInfo2").html($("#seatPos").html())
			$("#SeatInfo2").children(".seatItem").css("margin-left","20px")
			$("#SeatInfo2").prepend($("#SeatInfo2").children(".seatItem").length+"张")
			var orderTime= getCurrentDate(new Date())
			$("#orderId").text($("#orderId").text()+orderTime)
			$("#Order_number").val($("#orderId").text().substring(5,$("#orderId").text().length))
			$("#Actual_money").val(priceSum)
			$("#orderId3").text($("#orderId").text())
			$("#orderTime3").text(getCurrentDate2(new Date()))
			$("#paySum3").text(priceSum)
			$("#Order_time").val(getCurrentDate2(new Date()))
		}

	})
	function getCurrentDate(date){
		var y = date.getFullYear();
		var m = date.getMonth()+1;
		var d = date.getDate();
		var h = date.getHours();
		var min = date.getMinutes();
		var s = date.getSeconds();
		//var str=y+'年'+(m<10?('0'+m):m)+'月'+(d<10?('0'+d):d)+'日  '+(h<10?('0'+h):h)+':'+(min<10?('0'+min):min)+':'+(s<10?('0'+s):s);
		var str=y+(m<10?('0'+m):m)+(d<10?('0'+d):d)+(h<10?('0'+h):h)+(min<10?('0'+min):min)+(s<10?('0'+s):s);
		return str;
	}
	function getCurrentDate2(date){
		var y = date.getFullYear();
		var m = date.getMonth()+1;
		var d = date.getDate();
		var h = date.getHours();
		var min = date.getMinutes();
		var s = date.getSeconds();
		var str=y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d)+' '+(h<10?('0'+h):h)+':'+(min<10?('0'+min):min)+':'+(s<10?('0'+s):s);
		return str;
	}
	$("#orderConfirm").on("click",function(){

		var newSeat="";
		$("#two").hide();
		$("#three").show();
		$("#pro3").addClass("over")
		for (var i=0;i<$("#SeatInfo2").children(".seatItem").length;i++){

			var seatStr=$($("#SeatInfo2").children(".seatItem").get(i)).text()
			var hang = seatStr.substring(0,seatStr.indexOf("排"));
			var lie = seatStr.substring(2,seatStr.indexOf("座"));
			var orderId=$("#Order_number").val()
			var time_id=$("#time_id").val()
			alert("座位（"+seatStr+"）锁定成功")

				$.ajax({
					type:"post",
					data:{orderId:orderId, hang:hang,lie:lie,time_id:time_id,seat:seat},
					url:"http://127.0.0.1:8080/AddOrderSeat",
					success:function(data){
						//window.open ("pay.html");
						/*
                                            var scoun=0
                                            var seatArr=seat.split("")
                                            var num=0
                                            if(hang>3){
                                                num=3*12+(parseInt(hang)-3-1)*10+parseInt(lie)-1
                                            }else {
                                                num=(parseInt(hang)-1)*12+parseInt(lie)-1
                                            }
                                            alert(num+"numnumnum")
                                            for (var i=0;i<seatArr.length;i++){
                                                if(seatArr[i]==0||seatArr[i]==1){
                                                    scoun++;
                                                }

                                                if(scoun==num){
                                                    seatArr[i]="1";
                                                }
                                            }
                                            for (var i=0;i<seatArr.length;i++){
                                                newSeat=newSeat+seatArr[i]+""
                                            }
                                            alert(newSeat+"newSeatnewSeatnewSeatnewSeat")*/


					},

					error:function () {
						alert("错误");
					}
				});


		}

/*		$.ajax({
			type:"post",
			data:{newSeat :newSeat,time_id:time_id},
			url:"http://127.0.0.1:8080/updateSeat",
			success:function(data){


			},

			error:function () {
				alert("错误");
			}
		});*/
		//formpost("http://127.0.0.1:8080/",{'seat' :newSeat});
	})
	$("#backMain").on("click",function(){
		window.location.replace("show_main")
	})
	function formpost(url, postData) {
		var tempform = document.createElement("form");
		tempform.action = url;
		tempform.target = "_blank";
		tempform.method = "post";
		tempform.style.display = "none";
		for (var x in postData) {
			var opt = document.createElement("textarea");
			opt.name = x;
			opt.value = postData[x];
			tempform.appendChild(opt);
		}
		document.body.appendChild(tempform);
		tempform.submit();
	}
})